/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package invitation;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import validate.AccountValidate;

/**
 *
 * @author tomasz
 */
@Controller
public class EditInvitationForm {
    private dao.Firma firma;
    private dao.Account account;
    
    @Autowired
    public void setFirma(dao.Firma firma){
        this.firma = firma;
    }
    
    @Autowired
    public void setAccount(dao.Account account){
        this.account = account;
    }
    
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        
        SimpleDateFormat dateFormat = new SimpleDateFormat();
	    dateFormat.setLenient(false);       
	
	/**
	 * Date format - 9/18/2008 9:52:40
	 */
	dateFormat.applyPattern("yyyy-MM-dd");  
	
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        
    }
    
    @RequestMapping(value = "/edit/invitation.htm/{id}", method = RequestMethod.GET)
    public String EditInvitation(ModelMap model_map, @PathVariable Integer id) {
        
        model.Firma firma = this.firma.findFirmaId(id);
        model_map.addAttribute("firma", firma);
        
        return "invitation/edit_invitation";
        
    }
    
    @RequestMapping(value = "/show/invitation.htm/{id}", method = RequestMethod.GET)
    public String ShowInvitation(ModelMap model_map, @PathVariable Integer id) {
        
        model.Account account = this.account.findAccountId(id);
        model_map.addAttribute("id_acc", id);
        model_map.addAttribute(account);
        
        return "invitation/edit_show_invitation";
        
    }
    
    
    @RequestMapping(method= RequestMethod.POST)
    public String processSubmit(@ModelAttribute model.Account account, BindingResult result,HttpServletRequest request ){
        
               int id_firma = Integer.parseInt( request.getParameter("firma.idFirma"));
                
               this.account.editAccount(account); 
                
               return "redirect:/edit/invitation.htm/"+id_firma;
                
        
    }
    
}
